package com.yjs.app.trade.service.service.business.search.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yjs.app.trade.core.base.exception.BusinessException;
import com.yjs.app.trade.entity.base.BusinessConstant;
import com.yjs.app.trade.entity.base.SysConstant;
import com.yjs.app.trade.service.service.business.search.NewSearchListService;
import com.yjs.core.common.OperationResult;
import com.yjs.core.search.list.support.service.SearchListService;
import com.yjs.core.search.order.model.ApparContractSearchModel;
import com.yjs.core.search.order.model.ContractSearchModel;
import com.yjs.core.search.order.model.OrderSearchModel;
import com.yjs.list.model.ListParamBase;

@Service("newSearchListService")
public class NewSearchListServiceImpl implements NewSearchListService {

	@Autowired
	private SearchListService searchListService;

	/**
	 * 搜索条件map
	 * 
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> listSearchIndexs(Map<String, Object> map) {
		Map<String, Object> maps = new HashMap<>();
		OperationResult operationResult = null;
		try {
			operationResult = searchListService.listSearchIndexs(map.get("number").toString(),
					map.get("memberId").toString(), (int) map.get("memberType"), (List<ListParamBase>) map.get("list"),
					(PageBounds) map.get("pageBounds"));
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(SysConstant.ErrorCode.SYS_ERROR, "dubbo服务器异常!");
		}
		// 为订单的时候转为订单类，为合同的时候转化为合同类
		if (map.get("number").equals(BusinessConstant.Number.DRAGORDER)
				|| map.get("number").equals(BusinessConstant.Number.APPARORDER)) {
			PageList<OrderSearchModel> searchModels = (PageList<OrderSearchModel>) operationResult.getData();
			maps.put("data", searchModels);
			if (searchModels != null) {
				maps.put("page", searchModels.getPaginator().getTotalCount());
			}
			else {
				maps.put("page", 0);
			}
			maps.put("totalCountInfo", operationResult.getData2());
		} else if ( map.get("number").equals(BusinessConstant.Number.DRAGCONTRACT)) {
			PageList<ContractSearchModel> contractSearchModels = (PageList<ContractSearchModel>) operationResult
					.getData();
			/*if (contractSearchModels != null && contractSearchModels.size() > 0) {
				for (ContractSearchModel contractSearchModel : contractSearchModels) {
					if (contractSearchModel.getDrugPackage().indexOf("其他") != -1) {
						contractSearchModel.setDrugPackage("其他");
					}
				}
			}*/
			maps.put("data", contractSearchModels);
			if (contractSearchModels != null) {
				maps.put("page", contractSearchModels.getPaginator().getTotalCount());
			} else {
				maps.put("page", 0);
			}
		} else if (map.get("number").equals(BusinessConstant.Number.APPARCONTRACT)) {
			PageList<ApparContractSearchModel> contractSearchModels = (PageList<ApparContractSearchModel>) operationResult
					.getData();
			maps.put("data", contractSearchModels);
			if (contractSearchModels != null) {
				maps.put("page", contractSearchModels.getPaginator().getTotalCount());
			}
			else {
				maps.put("page", 0);
			}
		}
		return maps;
	}
	
	 /*
     * 获取搜索引擎的汇总字段
     
    @SuppressWarnings({ "unchecked", "rawtypes" })
	private Map<String, Object> getTotalCountMap(OperationResult results, Map<String, Object> totalCountMap, Map<String, ListParamBase> fileMap, Map<String, String> fieldNameTotalMap) {
        List<Map<String, Object>> returnData1 = new ArrayList<Map<String, Object>>();//返回的数据
        //data2获取值
        Object data2 = results.getData2();

        if (null != data2) {

            Map<String, FieldStatsInfo> datas2 = (Map<String, FieldStatsInfo>) data2;

            for (Map.Entry<String, FieldStatsInfo> env : datas2.entrySet()) {
                if (null == env) {
                    continue;
                }
                Map temp = new HashMap();
                //Map temp = SysUtils.classProperty2mapByCamelName(model,false);

                if (null == env.getKey() || "".equals(env.getKey())) {
                    continue;
                }

                String value = getValueFromJson(env.getValue(), env.getKey());

                temp.put(env.getKey(), value);
                returnData1.add(temp);
            }
        }

        if (null != returnData1 && returnData1.size() > 0) {
            returnData1 = ListHelper.formatDoubleToMoney(returnData1, fileMap);
            for (Map<String, Object> mapt : returnData1) {
                totalCountMap.putAll(mapt);
            }
        }
        return totalCountMap;
    }
    
    private String getValueFromJson(FieldStatsInfo value, String key, String totalWay) {
		
		 * sumPrice: {
						min: 2.0max: 159822.0sum: 894681.3count: 66missing: 0mean: 13555.777272727273stddev: 29622.64053809983
					}
		 
        if (null == value || null == key || "".equals(key) || null == totalWay || "".equals(totalWay)) {
            return null;
        }

        if (null == totalWay || "".equals(totalWay)) {
            return null;
        }

        String tempValue = "";
        if ("sum".equalsIgnoreCase(totalWay)) {
            tempValue = value.getSum() + "";
        } else if ("count".equalsIgnoreCase(totalWay)) {
            tempValue = value.getCount() + "";
        } else if ("min".equalsIgnoreCase(totalWay)) {
            tempValue = value.getMin() + "";
        } else if ("max".equalsIgnoreCase(totalWay)) {
            tempValue = value.getMax() + "";
        } else if ("mean".equalsIgnoreCase(totalWay)) {
            tempValue = value.getMean() + "";
        } else if ("countdistinct".equalsIgnoreCase(totalWay)) {
            tempValue = value.getCountDistinct() + "";
        } else if ("sumofsquares".equalsIgnoreCase(totalWay)) {
            tempValue = value.getSumOfSquares() + "";
        }
        if (!tempValue.equals("")) {
            tempValue = ListHelper.foundMoney(tempValue);
        }
        return tempValue;
    }*/

}
